<template>
  <div class="app-container">
    <!-- 查询条件 -->
    <div class="filter-container">
      <el-input v-model="listQuery.userId" clearable size="mini" class="filter-item" style="width: 200px;" placeholder="请输入用户ID"/>
      <el-input v-model="listQuery.publishSn" clearable size="mini" class="filter-item" style="width: 200px;" placeholder="请输入发布单编号"/>
      <el-select v-model="listQuery.status" size="mini" style="width: 200px" class="filter-item" placeholder="请选择状态">
        <el-option v-for="(key, value) in statusMap" :key="key" :label="key" :value="value"/>
      </el-select>
      <el-button v-permission="['GET /admin/publish/list']" size="mini" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
    </div>

    <!-- 发布单列表 -->
    <el-table v-loading="listLoading" :data="list" size="small" element-loading-text="正在查询中..." border fit highlight-current-row>
      <el-table-column align="center" label="发布单ID" prop="id" width="80"/>
      <el-table-column align="center" label="用户ID" prop="userId" width="80"/>
      <el-table-column align="center" label="服务类型" prop="serviceType" width="120"/>
      <el-table-column align="center" label="需求描述" prop="demandInfo" min-width="200"/>
      <el-table-column align="center" label="图片" prop="images" width="120">
        <template slot-scope="scope">
          <img v-if="scope.row.images" :src="scope.row.images" width="50">
        </template>
      </el-table-column>
      <el-table-column align="center" label="联系电话" prop="phone" width="120"/>
      <el-table-column align="center" label="状态" prop="status" width="100">
        <template slot-scope="scope">
          <el-tag>{{ scope.row.status | statusFilter }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建时间" prop="addTime" width="160"/>
      <el-table-column align="center" label="操作" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-permission="['POST /admin/publish/handle']" v-if="scope.row.status === 0" type="primary" size="mini" @click="handleAccept(scope.row)">接单</el-button>
          <el-button v-permission="['POST /admin/publish/handle']" v-if="scope.row.status === 1" type="danger" size="mini" @click="handleCancel(scope.row)">取消</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <!-- 发布单详情对话框 -->
    <el-dialog :visible.sync="detailDialogVisible" title="发布单详情" width="800">
      <el-form :model="detailData" label-position="left">
        <el-form-item label="发布单编号">
          <span>{{ detailData.publishSn }}</span>
        </el-form-item>
        <el-form-item label="用户信息">
          <span>{{ detailData.user.nickname }} (ID: {{ detailData.userId }})</span>
        </el-form-item>
        <el-form-item label="服务类型">
          <span>{{ detailData.serviceType }}</span>
        </el-form-item>
        <el-form-item label="需求描述">
          <span>{{ detailData.demandInfo }}</span>
        </el-form-item>
        <el-form-item label="图片信息">
          <img v-for="(img, index) in detailData.images" :key="index" :src="img" width="100" style="margin-right: 10px;">
        </el-form-item>
        <el-form-item label="地址信息">
          <span>{{ detailData.address }} {{ detailData.doorNumber }}</span>
        </el-form-item>
        <el-form-item label="联系电话">
          <span>{{ detailData.phone }}</span>
        </el-form-item>
        <el-form-item label="状态">
          <el-tag>{{ detailData.status | statusFilter }}</el-tag>
        </el-form-item>
        <el-form-item label="创建时间">
          <span>{{ detailData.addTime }}</span>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listPublish, handle } from '@/api/business/publish'
import Pagination from '@/components/Pagination'

const statusMap = {
  0: '待处理',
  1: '已接单',
  2: '已完成',
  3: '已取消'
}

export default {
  name: 'Publish',
  components: { Pagination },
  filters: {
    statusFilter(status) {
      return statusMap[status]
    }
  },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        userId: undefined,
        publishSn: undefined,
        status: undefined
      },
      statusMap,
      detailDialogVisible: false,
      detailData: {
        user: {},
        images: []
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      listPublish(this.listQuery).then(response => {
        // 修正API响应数据解析路径，处理两种可能的返回结构
        const data = response.data.data || { list: [], total: 0 }
        const list = Array.isArray(data) ? data : (data.list || [])
        const total = data.total || list.length
        // console.log('获取发布单列表:', JSON.stringify(list), '总数:', total)

        this.list = list.map(item => ({
          ...item,
          publishSn: item.id,
          images: item.images ? [item.images] : [],
          addTime: item.createTime || item.addTime,
          phone: item.phone
        }))
        this.total = total
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleAccept(row) {
      this.$confirm('确定接单此发布单吗？', '提示', { type: 'warning' }).then(() => {
        handle({ id: row.id, status: 1 }).then(() => {
          this.$message.success('接单成功')
          this.getList()
        })
      })
    },
    handleCancel(row) {
      this.$confirm('确定取消此发布单吗？', '提示', { type: 'warning' }).then(() => {
        handle({ id: row.id, status: 3 }).then(() => {
          this.$message.success('取消成功')
          this.getList()
        })
      })
    },
  }
}
</script>